package iiet.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import iiet.dao.TypeFee;
import iiet.service.IietService;
import iiet.util.IndurUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {

	@Autowired
	IietService iietService;

	@RequestMapping(value = "/paymentform")
	public String paymentForm() {
		System.out.println("HomeController: Passing through paymentform...");
		return "paymentform";
	}
	@RequestMapping(value = "/duesearchform")
	public String dueSearchForm() {
		System.out.println("HomeController: Passing through duesearchform...");
		return "duesearchform";
	}

	@RequestMapping(value = "/studentform")
	public String studentForm(Model model) {

		List<TypeFee> fees = iietService.findAllTypeFees();
		Map<String, Long> feeMap = new HashMap<String, Long>();
		for (TypeFee f : fees) {
			feeMap.put(f.getId().getType(), f.getId().getFee());
		}

		model.addAttribute("fees", feeMap);
		System.out.println("HomeController: Passing through studentform...");
		return "studentform";
	}

	@RequestMapping(value = "/savepayment", method = RequestMethod.POST)
	public String savePayment(
			@RequestParam("receipt_number") String receiptNumber,
			@RequestParam("htno") String htno,
			@RequestParam("year") String year,
			@RequestParam("tution_fee") String tutionFee,
			@RequestParam("admission_fee") String admissionFee,
			@RequestParam("hostel_fee") String hostelFee,
			@RequestParam("transport_fee") String transportFee,
			@RequestParam("paid_by") String paidBy, Model model) {
		System.out.println("HomeController: Passing through... display");
		try {
			iietService.savePayment(receiptNumber, htno, year,
					IndurUtils.toLong(tutionFee), IndurUtils.toLong(admissionFee),
					IndurUtils.toLong(hostelFee), IndurUtils.toLong(transportFee),
					paidBy);
			model.addAttribute("msg",
					iiet.constants.Constants.SAVED_SUCCESSFULLY);
		} catch (Exception e) {
			model.addAttribute("msg", e.getMessage());
		}

		return "display";

	}

	/*
	 * @RequestMapping(value = "/savestudent", method = RequestMethod.POST)
	 * public String saveStudent(
	 * 
	 * @RequestParam("htno") String htno, @RequestParam("name") String name,
	 * 
	 * @RequestParam("branch") String branch,
	 * 
	 * @RequestParam("father_name") String fatherName,
	 * 
	 * @RequestParam("mobile") String mobile,
	 * 
	 * @RequestParam("email") String email,
	 * 
	 * @RequestParam("parent_mobile1") String parent_mobile1,
	 * 
	 * @RequestParam("parent_mobile2") String parent_mobile2,
	 * 
	 * @RequestParam("address") String address, Model model) {
	 * System.out.println("HomeController: Passing through... saveStudent"); try
	 * { iietService.saveStudent( htno, name, branch, fatherName, mobile, email,
	 * parent_mobile1,parent_mobile2,address); model.addAttribute("msg",
	 * iiet.constants.Constants.SAVED_SUCCESSFULLY); } catch (Exception e) {
	 * model.addAttribute("msg", e.getMessage()); }
	 * 
	 * return "display"; }
	 */

	@RequestMapping(value = "/savestudent", method = RequestMethod.POST)
	public String saveStudent(@RequestParam("htno") String htno,
			@RequestParam("name") String name,
			@RequestParam("course") String course,
			@RequestParam("branch") String branch,
			@RequestParam("year") String year,
			@RequestParam("hostel") String hostel,
			@RequestParam("bus") String bus,

			@RequestParam("category") String category,
			@RequestParam("tution_fee") String tutionFee,
			@RequestParam("admission_fee") String admissionFee,
			@RequestParam("hostel_fee") String hostelFee,
			@RequestParam("transportation_fee") String transportFee,
			@RequestParam("total_fee") String totalFee,

			@RequestParam("father_name") String fatherName,
			@RequestParam("mobile") String mobile,
			@RequestParam("email") String email,
			@RequestParam("parent_mobile1") String parent_mobile1,
			@RequestParam("parent_mobile2") String parent_mobile2,
			@RequestParam("address") String address, Model model) {
		System.out.println("HomeController: Passing through... saveStudent");
		try {
			iietService.saveStudent(htno, name, course, branch, year, hostel,
					bus, category, IndurUtils.toLong(tutionFee), IndurUtils.toLong(admissionFee),
					IndurUtils.toLong(hostelFee), IndurUtils.toLong(transportFee), IndurUtils.toLong(
							totalFee), fatherName, mobile, email,
					parent_mobile1, parent_mobile2, address);
			model.addAttribute("msg",
					iiet.constants.Constants.SAVED_SUCCESSFULLY);
		} catch (Exception e) {
			model.addAttribute("msg", e.getMessage());
		}

		return "display";
	}
	@RequestMapping(value = "/searchdues")
	public String searchDues(@RequestParam("course") String course,
			@RequestParam("branch") String branch,
			@RequestParam("year") String year, Model model) {
		model.addAttribute("students", iietService.findAllDueStudents(course, branch, year));
		return "duelist";
	}
}